﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;

namespace zielonka.co.uk.extensions.system.drawing
{
    // Description:	Convert image to byte array
    /*
        Image image = ...;
        byte[] imageBytes = image.ToBytes(ImageFormat.Png);
     */

    public static partial class ImageExtensions
    {
        public static byte[] ToBytes(this Image image, ImageFormat format)
        {
            if (image == null)
                throw new ArgumentNullException("image");
            if (format == null)
                throw new ArgumentNullException("format");

            using (MemoryStream stream = new MemoryStream())
            {
                image.Save(stream, format);
                return stream.ToArray();
            }
        }
    }
}
